package helper

import (
	"fmt"
	"github.com/go-xorm/xorm"
	"log"
	"github.com/go-test/util"
	"github.com/go-xorm/core"
)

var dbEngine *xorm.Engine;
var dbError error;

/**
 * xorm reverse mysql root:root@(127.0.0.1:3306)/test?charset=utf8 E:\go_projects\src\github.com\go-xorm\cmd\xorm\templates\goxorm
 */

func XormEngine() *xorm.Engine {
	if dbEngine != nil {
		return dbEngine
	}
	configuration := system.GetConfiguration()
	params := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8&parseTime=true",
		configuration.User, configuration.Password, configuration.Host, configuration.DbName)
	log.Println("start init database config" + params)
	dbEngine, dbError = xorm.NewEngine(configuration.Driver, params)
	dbEngine.SetMaxIdleConns(50)
	dbEngine.SetMaxOpenConns(200)
	dbEngine.ShowSQL(true)
	dbEngine.Logger().SetLevel(core.LOG_INFO)
	if dbError != nil {
		log.Fatal(dbError)
		panic(dbError)
	}
	return dbEngine
}
